package com.finalcial.controller;



import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.finalcial.po.LoginMsg;
import com.finalcial.service.ApplicationService;
import com.finalcial.service.DeclarationInformationService;
import com.finalcial.service.WorkService;
import com.finalcial.service.WorkingDetailsService;
import com.finalcial.util.PageUtil;
/**
 * 
 * @author Lenovo
 *
 */
@Controller
@RequestMapping("/work")
public class WorkDoneController {
	
	@Resource
	private WorkService workService;
	@Resource
	private DeclarationInformationService declarationInformationService;
	@Resource
	private WorkingDetailsService detailsService;
	@Resource
	private ApplicationService applicationService;
	//显示已办界面
	@RequestMapping("/myWorkDone")
	public String listWorkMsg(HttpServletRequest request, HttpServletResponse response,HttpSession httpSession) {
		String workStateName = "已办";//来自界面
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userId = loginMsg.getUserId();	
		List<Map<String, Object>> workdonelist = null;
		String pageNow = request.getParameter("pageNow");
		PageUtil page = null;
		Integer totalCount = workService.listWorkMsgCounts(userId, workStateName);//总条数
		if(pageNow != null) {
			page = new PageUtil(totalCount, Integer.parseInt(pageNow));
			workdonelist = workService.listWorkMsg(userId, workStateName, page.getStartPos(), page.getPageSize());
		}else {
			page = new PageUtil(totalCount, 1);
			workdonelist = workService.listWorkMsg(userId, workStateName, page.getStartPos(), page.getPageSize());
		}
		/*for(Map<String, Object> map:workdonelist) {
			for(String s :map.keySet()) {
				System.out.println(map.get(s));
			}
		}*/
		request.setAttribute("page", page);
		request.setAttribute("workdonelist", workdonelist);
		return "datamanager/personwork/myWorkDone";
	}
	
	//点击某行数据显示信息
	@RequestMapping("/listWorkMsgDetails")
	public ModelAndView listWorkMsgDetails(HttpServletResponse response,HttpServletRequest request,HttpSession httpSession){
		ModelAndView mav = new ModelAndView();
		//显示全部的信息		
		//显示基础信息
		String reimbursementId  = request.getParameter("reimbursementId");
		String reimbursementTypeName = request.getParameter("reimbursementTypeName");
		System.out.println(reimbursementTypeName);
		/*try {
			reimbursementTypeName = new String(reimbursementTypeName.getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}*/
		String[] arrReimbursementTypeName = reimbursementTypeName.split("-");
		for(int i=0;i<arrReimbursementTypeName.length;i++){
			reimbursementTypeName = arrReimbursementTypeName[0];
		}
		String reimbursementName =reimbursementTypeName; 
		List<Map<String, Object>> listdetails = declarationInformationService.getEssentialInformation(reimbursementId);
		//内部人信息
		List<Map<String, Object>> listOtherInformation = declarationInformationService.getOtherInformation(reimbursementId);
		
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userId = loginMsg.getUserId();	
		//详细信息(非差旅)
		List<Map<String, Object>> listDetailInformation = detailsService.getDetailInformation(reimbursementId, userId);
		
		//详细信息(差旅)
		List<Map<String, Object>> listDetailInformationBytravel = detailsService.getDetailInformationBytravel(reimbursementId, userId);
		//解析图片文件
		List<Map<String, Object>> imageList = applicationService.getCheckSheet(reimbursementId);
				
		mav.addObject("listdetails", listdetails);
		mav.addObject("listOtherInformation", listOtherInformation);
		mav.addObject("listDetailInformation", listDetailInformation);
		mav.addObject("listDetailInformationBytravel", listDetailInformationBytravel);
		mav.addObject("imageList", imageList);

		if("国内差旅".equals(reimbursementName)){
			mav.setViewName("datamanager/personwork/domestictravel");
		}else if("公务交通".equals(reimbursementName)){
			mav.setViewName("datamanager/personwork/officialtraffic");
		}else if("物业管理".equals(reimbursementName)){
			mav.setViewName("datamanager/personwork/propertyManager");
		}else if("员工福利".equals(reimbursementName)){
			mav.setViewName("datamanager/personwork/staffform");
		}
				
		return mav;
	}
	
	//多条件查询已办
	@ResponseBody
	@RequestMapping("/chooseWorkDone")
	public List<Map<String, Object>> getWorkDoneMsg(HttpServletResponse response,HttpServletRequest request,HttpSession httpSession){
		String workStateName = "已办";//来自界面
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userId = loginMsg.getUserId();	
		String reimbursementTypeName = "";
		String userName = request.getParameter("userName");
		String moneyStart = request.getParameter("startMoney");
		String moneyEnd = request.getParameter("endMoney");
		String startTime = request.getParameter("startTime");
		System.out.println(startTime);
		String endTime = request.getParameter("endTime");
		System.out.println(endTime);
		List<Map<String, Object>> chooseWorkDoneList = workService.getWorkMsg(userId, workStateName, reimbursementTypeName, userName, moneyStart, moneyEnd, startTime, endTime);
		return chooseWorkDoneList;
		
	}
	
}
